/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
<template>
    <div class="platform_expiration-warn" :class="{'expired': expireFlag}">
        <lang :remain-date="expireTip" :version-type="versionType" v-if="!expireFlag">
            您的{version-type}还有{remain-date}到期，为避免业务中断，请及时续费授权
        </lang>
        <lang :version-type="versionType" v-else>
            您的{version-type}已到期，为避免业务中断，请及时续费授权
        </lang>

<!--        <span class="platform_expiration-warn_hide hand"-->
<!--              @click="toAuthEnterprise">-->
<!--            <lang>立即授权</lang>-->
<!--        </span>-->
<!--        <span class="platform_expiration-warn_hide hand"-->
<!--              @click="hideExpirationWarn">-->
<!--            <lang>不再提示</lang>-->
<!--        </span>-->
<!--        <span class="platform_expiration-warn_hide hand"-->
<!--              @click="onceHideExpirationWarn">-->
<!--            <lang>忽略本次</lang>-->
<!--        </span>-->
    </div>
</template>

<script>

/**
     * Created by czh on 2019/5/17.
     */

import AuthForm from 'src/home/mod_cluster/authorization/alerts/enterprise_auth_form.vue';
import IntervalTask from 'src/util/interval_task';
import {TOGGLE_AUTH_WARN} from 'src/vuex/mutation_types';
import urlAdapter from 'src/home/mod_common/url_adapter';
import {mapActions} from 'vuex';

const AUTH_ENTERPRISE_URL = urlAdapter('authorization/submit');   //授权企业版
const DISATANCE_TIME = 30; // 提前30天开始提醒用户
const ONE_HOUR_TIMESTEMP = '1000' * '60' * '60'; // 一小时更新请求一次

export default {
    props: {},

    data () {
        return {
            expireTip: '',
            isExpired: false
        };
    },

    computed: {
        isShowWarn () {
            return this.$store.state.authorization.isShowAuthWarn;
        },
        versionType () {
            return this.$store.getters.isProbationPeriod ? _('试用期') : _('授权');
        },
        expireFlag () {
            return this.$store.state.authorization.expireFlag;
        },
        expireDays () {
            return this.$store.state.authorization.expireDays;
        }
    },

    async created () {
        await this.isShowExpirationWarn();
        if (this.isShowWarn) {
            this._createIntervalTask();
        }
    },

    beforeDestroy () {
        if (this.authFormWin) {
            this.authFormWin.destroy();
        }
    },

    methods: {
        ...mapActions(['ajaxFetchAuthInfo']),

        async isShowExpirationWarn () {
            await this.ajaxFetchAuthInfo();
            this.expireTip = _('{0} 天', this.expireDays || 0);

            // 授权时间小于等于 30 天的时候提醒
            if (this.expireDays <= DISATANCE_TIME) {
                this.$store.commit(TOGGLE_AUTH_WARN, true);
            }
        },

        _createIntervalTask () {
            this.intervalTask = new IntervalTask({
                fn: async (next) => {
                    await this.isShowExpirationWarn();
                    next();
                },
                interval: ONE_HOUR_TIMESTEMP,
                skipFirst: true,
                autoStart: true
            });
        },

        startInterval () {
            this.intervalTask.start();
        },

        stopInterval () {
            this.intervalTask.stop();
        },

        _createAuthForm () {
            let authForm = this.$modal(AuthForm, {
                title: _('授权企业版')
            });

            // 惰性加载，生成唯一实例
            this._createAuthForm = () => authForm;
            return authForm;
        },

        // 授权企业版
        async toAuthEnterprise () {
            this.authFormWin = this._createAuthForm();
            this.authFormWin.open({
                reset: true,
                submit: async () => {
                    let params = this.authFormWin.formRoot.getJsonValue();
                    let jsonData = await this.$ajax.post(AUTH_ENTERPRISE_URL, params);

                    if (!jsonData.success) {
                        return this.$showErr(jsonData.message);
                    }

                    this.$ok(_('授权成功！'));
                    this.authFormWin.hide();
                }
            });
        },

        hideExpirationWarn () {
            this.$store.commit(TOGGLE_AUTH_WARN, false);
            this.stopInterval();
        },

        onceHideExpirationWarn () {
            this.$store.commit(TOGGLE_AUTH_WARN, false);
            this.stopInterval();
        }
    }
};

</script>

<style lang="less">
    .platform_expiration-warn {
        position: relative;
        background: #FFF2E0;
        color: #E80;
        height: 30px;
        line-height: 30px;
        text-align: center;
        font-size: 14px;

        &.expired {
            background: #FFEFEF;
            color: #CC5555;
        }

        .platform_expiration-warn_hide {
            border: 1px solid #FFF;
            border-radius: 20px;
            margin-left: 20px;
            padding: 2px 12px;
        }
    }

</style>
